package CCRMClient.GUI;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.SystemColor;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.CatalogManager;
import CCRMClient.Controllers.ClientCustomerManagement;
import Common.SERVER_COMMANDS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import net.proteanit.sql.DbUtils;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import java.awt.Component;
import java.io.IOException;
import java.sql.ResultSet;

import javax.swing.ListSelectionModel;
/**
 * This class implements Search Customer screen
 * @author Ola
 */
public class SearchCustomerScreen extends JPanel {
	private JLabel lblSearchCustomer;
	private JLabel lblCustomerName;
	private JTextField textSearchField;
	private JButton btnSearch;
	private JButton btnQuit;
	private JTable tblCustomers;
	private JScrollPane scpCustomers;
	private JLabel lblSearchResults;
	
	/**
	 * Create the panel.
	 */
	public SearchCustomerScreen() {
		initialize();
	}
	public SearchCustomerScreen(ResultSet searchResults, String search){
		this();
		textSearchField.setText(search);
		loadRStoTable(searchResults);
		tblCustomers.setEnabled(true);
		
	}
	
	private void loadRStoTable(ResultSet searchResults) {
		
		TableModel tableModel = DbUtils.resultSetToTableModel(searchResults);
		tblCustomers.setModel(tableModel);
	
		JTableHeader th = tblCustomers.getTableHeader();
		TableColumnModel tcm = th.getColumnModel();
		TableColumn tc = tcm.getColumn(0);
		tc.setHeaderValue("ID");
		
		th = tblCustomers.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(1);
		tc.setHeaderValue("Name");
		
		th = tblCustomers.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(2);
		tc.setHeaderValue("Birthday");
		
		th = tblCustomers.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(3);
		tc.setHeaderValue("Phone");
		
		th = tblCustomers.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(4);
		tc.setHeaderValue("Address");
		
		th = tblCustomers.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(5);
		tc.setHeaderValue("Status");
		
		th = tblCustomers.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(6);
		tc.setHeaderValue("RegionID");
		
		tblCustomers.getColumnModel().getColumn(0).setResizable(false);
		tblCustomers.getColumnModel().getColumn(0).setPreferredWidth(37);
		tblCustomers.getColumnModel().getColumn(0).setMinWidth(7);
		tblCustomers.getColumnModel().getColumn(2).setResizable(false);
		tblCustomers.getColumnModel().getColumn(2).setPreferredWidth(45);
		tblCustomers.getColumnModel().getColumn(2).setMinWidth(10);
		tblCustomers.getColumnModel().getColumn(4).setPreferredWidth(100);
		tblCustomers.getColumnModel().getColumn(5).setPreferredWidth(10);
		tblCustomers.getColumnModel().getColumn(6).setPreferredWidth(25);

		
		

		th.repaint();
	}
	
	private void initialize() {
		setSize(750, 600);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblSearchCustomer());
		add(getLblCustomerName());
		add(getTextSearchField());
		add(getBtnSearch());
		add(getBtnQuit());
		add(getLblSearchResults());
		add(getScpCustomers());
	}
	
	
	JLabel getLblSearchCustomer() {
		if (lblSearchCustomer == null) {
			lblSearchCustomer = new JLabel("Search Customer");
			lblSearchCustomer.setHorizontalAlignment(SwingConstants.CENTER);
			lblSearchCustomer.setForeground(Color.RED);
			lblSearchCustomer.setFont(new Font("David", Font.BOLD, 26));
			lblSearchCustomer.setBounds(59, 31, 637, 27);
		}
		return lblSearchCustomer;
	}
	JLabel getLblCustomerName() {
		if (lblCustomerName == null) {
			lblCustomerName = new JLabel("Search parametr:");
			lblCustomerName.setFont(new Font("David", Font.BOLD, 20));
			lblCustomerName.setBounds(36, 68, 154, 25);
		}
		return lblCustomerName;
	}
	
	JTextField getTextSearchField() {
		if (textSearchField == null) {
			textSearchField = new JTextField();
			textSearchField.setText("fill in customer data or ID");
			textSearchField.setColumns(10);
			textSearchField.setBackground(SystemColor.menu);
			textSearchField.setBounds(240, 69, 251, 20);
		}
		return textSearchField;
	}
	
	JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new BtnSearchActionListener());
			btnSearch.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSearch.setBounds(541, 69, 124, 23);
		}
		return btnSearch;
	}
	
	JButton getBtnQuit() {
		if (btnQuit == null) {
			btnQuit = new JButton("Quit");
			btnQuit.addActionListener(new BtnQuitkActionListener());
			btnQuit.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnQuit.setBounds(61, 510, 124, 23);
		}
		return btnQuit;
	}
	JLabel getLblSearchResults() {
		if (lblSearchResults == null) {
			lblSearchResults = new JLabel("Search Results:");
			lblSearchResults.setHorizontalAlignment(SwingConstants.LEFT);
			lblSearchResults.setForeground(Color.BLACK);
			lblSearchResults.setFont(new Font("David", Font.BOLD, 22));
			lblSearchResults.setBounds(46, 110, 154, 27);
		}
		return lblSearchResults;
	}
	JTable getTblCustomers() {
		if (tblCustomers == null) {
			tblCustomers = new JTable();
			tblCustomers.setBackground(SystemColor.menu);
			tblCustomers.setFont(new Font("David", Font.BOLD, 11));
			tblCustomers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblCustomers.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
			tblCustomers.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null,null,null},
				},
				new String[] {
					"ID", "Name", "Brithday", "Phone", "Address","Status","regionID"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class, String.class, String.class,String.class,String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
			});
			tblCustomers.getColumnModel().getColumn(0).setResizable(false);
			tblCustomers.getColumnModel().getColumn(0).setPreferredWidth(37);
			tblCustomers.getColumnModel().getColumn(0).setMinWidth(7);
			tblCustomers.getColumnModel().getColumn(2).setResizable(false);
			tblCustomers.getColumnModel().getColumn(2).setPreferredWidth(45);
			tblCustomers.getColumnModel().getColumn(2).setMinWidth(10);
			tblCustomers.getColumnModel().getColumn(4).setPreferredWidth(100);
			tblCustomers.getColumnModel().getColumn(5).setPreferredWidth(10);
			tblCustomers.getColumnModel().getColumn(6).setPreferredWidth(25);
		}
		return tblCustomers;
	}
	
	JScrollPane getScpCustomers() {
		if (scpCustomers == null) {
			scpCustomers = new JScrollPane();
			scpCustomers.setViewportBorder(null);
			scpCustomers.setBounds(79, 148, 617, 351);
			scpCustomers.setViewportView(getTblCustomers());
		}
		return scpCustomers;
	}
	/**
	 * This inner class implements event listener for pressing Quit button
	 * @author Ola
	 *
	 */
	private class BtnQuitkActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new CustomerManage());
		}
	}
	/**
	 * This inner class implements event listener for pressing search button
	 * @author Ola
	 *
	 */
	private class BtnSearchActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				String search = textSearchField.getText();
			if (search.isEmpty() || search.equals("fill in customer data or ID")) throw new NullPointerException();
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.GET_CUSTOMER_BY_CONDITION,search);
			} catch (IOException exp) {
				JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
				ClientStart.quitApp();
			}
				
			}catch (NullPointerException exp) {
				JOptionPane.showMessageDialog(null, "fill in customer data or ID", "Search Customer", JOptionPane.INFORMATION_MESSAGE);
				textSearchField.setText("fill in customer data or ID");
			}
		}
}
}
